﻿package base
{
	import com.dusk.util.ResUtil;
	
	import flash.display.*;
	import flash.geom.*;
	import flash.utils.*;
	
	public class BaseBitmapDataPool
	{
		
		public static var infoDict:Dictionary = new Dictionary();
		
		public static function getBitmapDataArrayByName(param1:String):Array
		{
			var _loc2_:* = null;
			if (hasRegisteredData(param1))
			{
				return infoDict[param1];
			}
			_loc2_ = registerData(param1);
			if (_loc2_)
			{
				return _loc2_;
			}
			return null;
		}
		
		public static function getRoleBitmapDataArrayByName(param1:String):Array
		{
			var _loc1_:Array = null;
			if (!hasRegisteredData(param1))
			{
				_loc1_ = registerRoleData(param1);
				return _loc1_;
			}
			return infoDict[param1];
		}
		
		public static function registerData(param1:String):Array
		{
			var str:String = param1;
			var tarObj:* = null;
			var arr:* = null;
			var arr2:* = null;
			var bmd1:* = null;
			var bmd2:* = null;
			if (hasRegisteredData(str))
			{
				return infoDict[str];
			}
			try
			{
				tarObj = ResUtil.getNewObj(str);
			} catch (e:Error)
			{
				throw(e);
			}
			if (tarObj is BitmapData)
			{
				arr = [];
				bmd1 = tarObj as BitmapData;
				arr.push(bmd1);
				bmd2 = new BitmapData(bmd1.width, bmd1.height, true, 0);
				bmd2.draw(bmd1, new Matrix(-1, 0, 0, 1, bmd1.width, 0));
				arr.push(bmd2);
				infoDict[str] = arr;
				return arr;
			}
			if (tarObj is MovieClip)
			{
				arr2 = analysisMcToBitmapDataArray(tarObj as MovieClip);
				infoDict[str] = arr2;
				return arr2;
			}
			return null;
		}
		
		public static function registerRoleData(param1:String):Array
		{
			if (hasRegisteredData(param1))
			{
				return infoDict[param1];
			}
			var _loc1_:Vector.<String> = new <String>["ROLE1_", "ROLE2_", "ROLE3_","ROLE4_"];
			var _loc2_:String = "";
			var _loc3_:* = null;
			var showId:int;
			var tarObj:* = null;
			for each(var item:String in _loc1_)
			{
				if (param1.indexOf(item) != -1)
				{
					_loc2_ += item;
					break;
				}
			}
			if (param1.indexOf("EQUIP_") != -1)
			{
				_loc2_ += "EQUIP_";
			}
			showId = param1.split("_").reverse()[0];
			try
			{
				tarObj = ResUtil.getNewObj(_loc2_.substr(0, _loc2_.length - 1));
			} catch (e:*)
			{
				throw(e);
			}
			if (tarObj)
			{
				_loc3_ = analysisRoleMcToBitmapDataArray((tarObj as MovieClip), showId);
				infoDict[param1] = _loc3_;
				return _loc3_;
			}
			
			return null;
		}
		
		public static function analysisMcToBitmapDataArray(param1:MovieClip):Array
		{
			var _loc2_:int = 0;
			var _loc3_:* = null;
			var _loc4_:* = 0;
			var _loc5_:int = 0;
			var _loc6_:* = null;
			var _loc7_:* = null;
			var _loc8_:* = null;
			var _loc9_:* = [];
			param1.gotoAndStop(1);
			var _loc10_:uint = param1.totalFrames;
			var _loc11_:* = [];
			var _loc12_:* = [];
			_loc2_ = 1;
			while (_loc2_ <= _loc10_)
			{
				_loc11_[_loc2_ - 1] = [];
				_loc12_[_loc2_ - 1] = [];
				param1.gotoAndStop(_loc2_);
				_loc3_ = param1.getChildAt(0) as MovieClip;
				if (_loc3_)
				{
					_loc4_ = uint(_loc3_.totalFrames);
					_loc5_ = 1;
					while (_loc5_ <= _loc4_)
					{
						_loc3_.gotoAndStop(_loc5_);
						_loc6_ = new BitmapData(param1.width, param1.height, true, 0xFFFFFF);
						_loc7_ = new BitmapData(param1.width, param1.height, true, 0xFFFFFF);
						_loc8_ = _loc3_.getBounds(param1);
						_loc6_.draw(_loc3_, new Matrix(1, 0, 0, 1, -Number(_loc8_.x) + _loc3_.x, -Number(_loc8_.y) + _loc3_.y), null, null, null, true);
						_loc11_[_loc2_ - 1][_loc5_ - 1] = _loc6_;
						_loc7_.draw(_loc3_, new Matrix(-1, 0, 0, 1, _loc8_.x + _loc8_.width - Number(_loc3_.x), -Number(_loc8_.y) + _loc3_.y), null, null, null, true);
						_loc12_[_loc2_ - 1][_loc5_ - 1] = _loc7_;
						_loc5_++;
					}
				}
				_loc2_++;
			}
			_loc9_.push(_loc11_, _loc12_);
			return _loc9_;
		}
		
		public static function analysisRoleMcToBitmapDataArray(param1:MovieClip, param2:int):Array
		{
			var i:int = 0;
			var _loc2_:int = 0;
			var _loc3_:* = null;
			var _loc4_:* = 0;
			var _loc5_:int = 0;
			var _loc6_:* = null;
			var _loc7_:* = null;
			var _loc8_:* = null;
			var _loc9_:* = [];
			param1.gotoAndStop(1);
			var _loc10_:uint = param1.totalFrames;
			var _loc11_:* = [];
			var _loc12_:* = [];
			_loc2_ = 1;
			while (_loc2_ <= _loc10_)
			{
				_loc11_[_loc2_ - 1] = [];
				_loc12_[_loc2_ - 1] = [];
				param1.gotoAndStop(_loc2_);
				_loc3_ = param1.getChildAt(0) as MovieClip;
				if (_loc3_)
				{
					_loc4_ = uint(_loc3_.totalFrames);
					_loc5_ = 1;
					while (_loc5_ <= _loc4_)
					{
						_loc3_.gotoAndStop(_loc5_);
						i = 0;
						while (i < _loc3_.numChildren)
						{
							if (_loc3_.getChildAt(i) is MovieClip)
							{
								try
								{
									_loc3_.getChildAt(i).gotoAndStop("show" + param2);
								} catch (e:Error)
								{
									_loc3_.getChildAt(i).gotoAndStop(1);
								}
							}
							i++;
						}
						_loc6_ = new BitmapData(param1.width, param1.height, true, 0xFFFFFF);
						_loc7_ = new BitmapData(param1.width, param1.height, true, 0xFFFFFF);
						_loc8_ = _loc3_.getBounds(param1);
						_loc6_.draw(_loc3_, new Matrix(1, 0, 0, 1, -Number(_loc8_.x) + _loc3_.x, -Number(_loc8_.y) + _loc3_.y), null, null, null, true);
						_loc11_[_loc2_ - 1][_loc5_ - 1] = _loc6_;
						_loc7_.draw(_loc3_, new Matrix(-1, 0, 0, 1, _loc8_.x + _loc8_.width - Number(_loc3_.x), -Number(_loc8_.y) + _loc3_.y), null, null, null, true);
						_loc12_[_loc2_ - 1][_loc5_ - 1] = _loc7_;
						_loc5_++;
					}
				}
				_loc2_++;
			}
			_loc9_.push(_loc11_, _loc12_);
			return _loc9_;
		}
		
		public static function hasRegisteredData(param1:String):Boolean
		{
			return infoDict[param1] != undefined && infoDict[param1] != null;
		}
		
		public static function removeData(param1:String):void
		{
			var _loc2_:* = null;
			var _loc3_:int = 0;
			var _loc4_:Array;
			_loc4_ = infoDict[param1] as Array;
			if (_loc4_)
			{
				_loc3_ = 0;
				while (_loc3_ < _loc4_.length)
				{
					if (_loc4_[_loc3_] is BitmapData)
					{
						_loc2_ = _loc4_[_loc3_] as BitmapData;
						_loc2_.dispose();
						_loc2_ = null;
					}
					else if (_loc4_[_loc3_] is Array)
					{
						(_loc4_[_loc3_] as Array).length = 0;
					}
					_loc3_++;
				}
			}
			infoDict[param1] = null;
			delete infoDict[param1];
			/*trace("=========infoDict==========");
			var _loc6_:* = infoDict;
			for (_loc7_ in infoDict)
			{
				trace("key:" + _loc7_);
			}*/
		}
		
		public static function reomveRoleAndSutraData():void
		{
			var _loc4_:* = undefined;
			var _loc7_:* = undefined;
			var _loc1_:* = [];
			for (_loc4_ in infoDict)
			{
				if (_loc4_.indexOf("ROLE1_") != -1 || _loc4_.indexOf("ROLE2_") != -1 || _loc4_.indexOf("ROLE3_") != -1 || _loc4_.indexOf("ROLE4_") != -1 || _loc4_.indexOf("Magic") != -1)
				{
					_loc1_.push(_loc4_);
				}
			}
			for each (_loc7_ in _loc1_)
			{
				removeData(_loc7_);
			}
			_loc1_.length = 0;
		}
	}
}
